package com.yserp.project.erp.order.deliveryorder.controller;


import com.yserp.common.utils.poi.ExcelUtil;
import com.yserp.framework.aspectj.lang.annotation.Log;
import com.yserp.framework.aspectj.lang.enums.BusinessType;
import com.yserp.framework.web.controller.BaseController;
import com.yserp.framework.web.domain.AjaxResult;
import com.yserp.framework.web.page.TableDataInfo;
import com.yserp.project.erp.common.enums.OrderEnums;
import com.yserp.project.erp.order.deliveryorder.domain.DeliveryOrder;
import com.yserp.project.erp.order.deliveryorder.domain.DeliveryOrderDto;
import com.yserp.project.erp.order.deliveryorder.domain.DeliveryOrderVo;
import com.yserp.project.erp.order.deliveryorder.service.IDeliveryOrderService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * 销售发货订单Controller
 *
 * @author yserp
 * @date 2022-07-26
 */
@Controller
@RequestMapping("/order/deliveryorder")
public class DeliveryOrderController extends BaseController {
    private String prefix = "erp/order/deliveryorder";

    @Autowired
    private IDeliveryOrderService deliveryOrderService;

    @RequiresPermissions("order:deliveryorder:view")
    @GetMapping()
    public String deliveryorder() {
        return prefix + "/deliveryorder";
    }

    /**
     * 查询销售发货订单列表
     */
    @RequiresPermissions("order:deliveryorder:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DeliveryOrder deliveryOrder) {
        startPage();
        List<DeliveryOrder> list = deliveryOrderService.selectDeliveryOrderList(deliveryOrder);
        return getDataTable(list);
    }

    /**
     * 导出销售发货订单列表
     */
    @RequiresPermissions("order:deliveryorder:export")
    @Log(title = "销售发货订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DeliveryOrder deliveryOrder) {
        List<DeliveryOrder> list = deliveryOrderService.selectDeliveryOrderList(deliveryOrder);
        ExcelUtil<DeliveryOrder> util = new ExcelUtil<DeliveryOrder>(DeliveryOrder.class);
        return util.exportExcel(list, "销售发货订单数据");
    }

    /**
     * 获取单号
     */
    @GetMapping("/getOrderNo")
    @ResponseBody
    public AjaxResult getOrderNo() {
        return AjaxResult.success("",OrderEnums.getCacheOrderNo(OrderEnums.DELIVERY_ORDER.getPref()));
    }

    /**
     * 新增销售发货订单
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        mmap.put("deliveryOrderNo", OrderEnums.getCacheOrderNo(OrderEnums.DELIVERY_ORDER.getPref()));
        return prefix + "/add";
    }

    /**
     * 新增保存销售发货订单
     */
    @RequiresPermissions("order:deliveryorder:add")
    @Log(title = "销售发货订单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DeliveryOrderDto deliveryOrderDto) {
        return toAjax(deliveryOrderService.insertDeliveryOrder(deliveryOrderDto));
    }

    /**
     * 修改销售发货订单
     */
    @RequiresPermissions("order:deliveryorder:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        DeliveryOrderVo deliveryOrderVo = deliveryOrderService.selectDeliveryOrderById(id);
        mmap.put("deliveryOrderVo", deliveryOrderVo);
        return prefix + "/edit";
    }

    /**
     * 修改保存销售发货订单
     */
    @RequiresPermissions("order:deliveryorder:edit")
    @Log(title = "销售发货订单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DeliveryOrderDto deliveryOrderDto) {
        return toAjax(deliveryOrderService.updateDeliveryOrder(deliveryOrderDto));
    }

    /**
     * 删除销售发货订单
     */
    @RequiresPermissions("order:deliveryorder:remove")
    @Log(title = "销售发货订单", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(deliveryOrderService.deleteDeliveryOrderByIds(ids));
    }

    /**
     * 审核销售发货订单
     */
    @RequiresPermissions("order:deliveryorder:audit")
    @Log(title = "销售发货订单", businessType = BusinessType.AUDIT)
    @PostMapping("/audit")
    @ResponseBody
    public AjaxResult audit(String ids) {
        return toAjax(deliveryOrderService.auditDeliveryOrderByIds(ids));
    }

    /**
     * 确认发货销售发货订单
     */
    @PostMapping("/deliveryStatus")
    @ResponseBody
    public AjaxResult deliveryStatus(DeliveryOrder deliveryOrder) {
        return AjaxResult.success(deliveryOrderService.deliveryStatus(deliveryOrder));
    }
}
